package cn.itsource.order.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.itsource.alipay.aplipayCommon.AlipayService;
import cn.itsource.basic.config.BusinessException;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.GetUserInfoUtil;
import cn.itsource.order.domain.OrderAddress;
import cn.itsource.order.domain.OrderAdopt;
import cn.itsource.order.domain.OrderDto;
import cn.itsource.order.domain.UserAddress;
import cn.itsource.order.mapper.OrderAddressMapper;
import cn.itsource.order.mapper.OrderAdoptMapper;
import cn.itsource.order.mapper.UserAddressMapper;
import cn.itsource.order.service.IOrderAdoptService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.mapper.PetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;


@Service
@Transactional
public class OrderAdoptServiceImpl extends BaseServiceImpl<OrderAdopt, BaseQuery> implements IOrderAdoptService{

    @Autowired
    private OrderAdoptMapper orderAdoptMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private PetMapper petMapper;

    @Autowired
    private AlipayService alipayService;

    /**
     * 提交订单
     * @param orderDto
     * @return
     */
    @Override
    public AjaxResult submitOrder(OrderDto orderDto) {

        Long petId = orderDto.getPetId();
        //判断如果当前这个宠物有未支付的支付,别人不能下单
        //OrderAdopt orderAdopt1 = orderAdoptMapper.findOrderByPetId(petId);
//        if (orderAdopt1 != null){
//            throw new BusinessException("已被下单,刷新页面");
//        }

        Long addressId = orderDto.getAddressId();

        //1.通过用户地址id,拿到用户地址对象
        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(addressId);
        //2.把用户地址对象的数据copy到订单地址对象中
        OrderAddress orderAddress = BeanUtil.copyProperties(userAddress, OrderAddress.class);
        orderAddress.setId(null);

        //3.把订单地址入库,拿到主键id
        orderAddressMapper.insertSelective(orderAddress);

        //3.1查询宠物对象
        Pet pet = petMapper.selectByPrimaryKey(petId);

        //4.组装订单对象的数据,并且把订单地址的id设置进去
        OrderAdopt orderAdopt = new OrderAdopt();
        orderAdopt.setDigest("用户地址id:"+addressId+",宠物id:"+petId);
        orderAdopt.setState(0);//待支付
        Double jg = (pet.getSaleprice()*2 + 100 + 1000);
        orderAdopt.setPrice(BigDecimal.valueOf(jg));
        String orderSn = new Snowflake().nextIdStr();
        orderAdopt.setOrderSn(orderSn);
        orderAdopt.setLastConfirmTime(new Date());
        orderAdopt.setPetId(petId);
        orderAdopt.setUserId(GetUserInfoUtil.getUserId());
        orderAdopt.setShopId(pet.getShopId());
        orderAdopt.setAddressId(orderAddress.getId());

        //5.把订单入库
        orderAdoptMapper.insertSelective(orderAdopt);

        //6.调用生成支付页面的form的方法 todo  重点
        String payUrl = alipayService.createPayUrl(jg,"上当了哈哈哈",orderSn);
        return AjaxResult.createSuccess(payUrl);
    }
}
